﻿using ETModel;
using System.Collections.Generic;
using UnityEngine;

namespace ETHotfix
{
	[MessageHandler]
	public class M2C_PathfindingResultHandler : AMHandler<M2C_PathfindingResult>
	{
		protected override void Run(ETModel.Session session, M2C_PathfindingResult message)
		{
			var unit = Game.Scene.GetComponent<UnitsComponent>().Get(message.UnitId);

            var ani = unit.GetComponent<AnimatorComponent>();
            var unitPathComponent = unit.GetComponent<UnitPathComponent>();

            
            ani?.SetFloatValue("Speed", 5f);

            var movePath = new List<Vector3>();
            for (int i = 0; i < message.Xs.Count; ++i) {
                movePath.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i]));
            }
            var startPos = new Vector3(message.X, message.Y, message.Z);
            var moveTask = unitPathComponent.StartMove(startPos, movePath, 5f);
            moveTask.GetAwaiter().OnCompleted(()=> {
                ani?.SetFloatValue("Speed", 0f);
            });


            //把移动路径信息输出调试
            GizmosDebug.Instance.Path.Clear();
			GizmosDebug.Instance.Path.Add(new Vector3(message.X, message.Y, message.Z));
			for (int i = 0; i < message.Xs.Count; ++i)
			{
				GizmosDebug.Instance.Path.Add(new Vector3(message.Xs[i], message.Ys[i], message.Zs[i]));
			}
		}
	}
}
